import { uartSpeed, dataArr, checkArr, stopArr } from './TypeModel'
// @ts-ignore
import hisc from '@ohos.uart_napi'
import Hilog from '../Utils/HiLogUtils'


export class UartModel {
  public static instance: UartModel;
  private portList: SelectOption[] = [] //波特率
  private dataList: SelectOption[] = [] //数据位
  private checkList: SelectOption[] = [] //校验位
  private stopList: SelectOption[] = [] //停止位
  private devList: SelectOption[] = [] //串口位置

  private constructor() {
  }

  static getInstance(): UartModel {
    if (!UartModel.instance) {
      UartModel.instance = new UartModel()
    }
    return UartModel.instance
  }

  //初始化列表数据
  initPortData() {
    if (this.portList.length == 0) {
      for (let i = 0; i < uartSpeed.length; i++) {
        this.portList.push({ 'value': `${uartSpeed[i]}` })
      }
    }
    if (this.dataList.length == 0) {
      for (let i = 0; i < dataArr.length; i++) {
        this.dataList.push({ 'value': `${dataArr[i]}` })
      }
    }
    if (this.checkList.length == 0) {
      for (let i = 0; i < checkArr.length; i++) {
        this.checkList.push({ 'value': `${checkArr[i]}` })
      }
    }
    if (this.stopList.length == 0) {
      for (let i = 0; i < stopArr.length; i++) {
        this.stopList.push({ 'value': `${stopArr[i]}` })
      }
    }
    // // @ts-ignore
    // let res: string = hisc.queryUart()
    // let Arr = res.split(' ')
    // Hilog.info(`queryUart res = ${JSON.stringify(Arr)}`)
    // if (this.devList.length == 0) {
    //   for (let i = 0; i < Arr.length; i++) {
    //     this.devList.push({ 'value': `${Arr[i]}` })
    //   }
    //   this.devList.push({ 'value': `/dev/ttyUSB0` })
    // }
    this.devList.push({ 'value': `/dev/ttyUSB0` })
    this.devList.push({ 'value': `/dev/ttyS3` })
    this.devList.push({ 'value': `/dev/ttyUSB1` })
    this.devList.push({ 'value': `/dev/ttyUSB2` })
    this.devList.push({ 'value': `/dev/ttyUSB3` })
    this.devList.push({ 'value': `/dev/ttyUSB4` })
  }

  //获取串口波特率列表
  getPortList(): SelectOption[] {
    return this.portList
  }

  //获取数据位列表
  getDataList(): SelectOption[] {
    return this.dataList
  }

  //获取校验位列表
  getCheckList(): SelectOption[] {
    return this.checkList
  }

  //获取停止位列表
  getStopList(): SelectOption[] {
    return this.stopList
  }

  //获取串口列表
  getDevList(): SelectOption[] {
    return this.devList
  }

  //注册串口回调
  dataCallbackListener(callback: (data: string) => void) {
    Hilog.info(`UartTest > dataCallbackListener > start`)
    // hisc.registerEventCallback((data: string) => {
    //   callback(data)
    // })
  }

  //打开串口
  openUart(): number {
    return hisc.openUart()
  }

  //关闭串口
  closeUart(): number {
    return hisc.closeUart()
  }

  //设置串口参数
  setUartParams(param: string): number {
    return hisc.setUartParams(param)
  }

  //发送数据
  sendUartData(data: string): number {
    return hisc.sendUartData(data)
  }
}

class SelectOption {
  // @ts-ignore
  value: ResourceStr = '';

  // @ts-ignore
  icon?: ResourceStr = '';
}